<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <!-- 添加分类按钮 -->
      <el-row>
        <el-col>
          <el-button @click="addcate" type="primary">添加分类</el-button>
        </el-col>
      </el-row>

      <!-- 表格 -->
      <tree-table
        :data="catelist"
        :columns="columns"
        :selection-type="false"
        :expand-type="false"
        :show-index="true"
        index-text="#"
        border
        :show-row-hover="false"
        class="treeTable"
      >
        <template slot="valid" slot-scope="scope">
          <i
            class="el-icon-circle-check"
            v-if="scope.row.cat_deleted === true"
            style="color: green"
          ></i>
          <i
            class="el-icon-circle-close"
            v-if="scope.row.cat_deleted === false"
            style="color: red"
          ></i>
        </template>
        <template slot="order" slot-scope="scope">
          <el-tag size="mini" v-if="scope.row.cat_level === 0">一级</el-tag>
          <el-tag size="mini" v-if="scope.row.cat_level === 1" type="success"
            >二级</el-tag
          >
          <el-tag size="mini" v-if="scope.row.cat_level === 2" type="warning"
            >三级</el-tag
          >
        </template>
        <template slot="opt">
          <el-button size="mini" type="primary" icon="el-icon-edit"
            >编辑</el-button
          >
          <el-button size="mini" type="danger" icon="el-icon-delete"
            >删除</el-button
          >
        </template>
      </tree-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[3, 5, 10, 15]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 添加分类对话框 -->
    <el-dialog title="添加分类" :visible.sync="catedialogVisible" width="50%">
      <!-- 添加分类表单 -->
      <el-form
        :model="addcateForm"
        :rules="addcateFormRules"
        ref="addcateRef"
        label-width="100px"
      >
        <el-form-item label="分类名称:" prop="cat_name">
          <el-input v-model="addcateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类:">
          <el-cascader
            v-model="selectedKeys"
            :options="parentCateList"
            :props="{
              expandTrigger: 'hover',
              value: 'cat_id',
              label: 'cat_name',
              children: 'children',
            }"
            @change="parentCateChanged"
            clearable
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="catedialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addcates">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default ({
  data () {
    return {
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      // 商品分类的数据列表
      catelist: [],
      total: 0,
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        }, {
          label: '是否有效',
          type: 'template',
          template: 'valid'
        }, {
          label: '排序',
          type: 'template',
          template: 'order'
        }, {
          label: '操作',
          type: 'template',
          template: 'opt'
        }
      ],
      catedialogVisible: false,
      addcateForm: {
        cat_name: '',
        cat_pid: 0,
        cat_level: 0
      },
      addcateFormRules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' }
        ]
      },
      parentCateList: [],
      selectedKeys: []
    }
  },
  methods: {
    // 获取商品分类数据
    async getCatelist () {
      const { data: res } = await this.$http.get('categories', { params: this.queryInfo })

      if (res.meta.status !== 200) {
        return this.$message.error('获取商品分类数据失败')
      }

      this.catelist = res.data.result
      this.total = res.data.total
    },

    // 监听pagesize改变
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.getCatelist()
    },

    // 监听 pagenum改变
    handleCurrentChange (newPage) {
      this.queryInfo.pagenum = newPage
      this.getCatelist()
    },

    // 添加分类
    addcate () {
      this.getParent()
      this.catedialogVisible = true
    },

    // 获取父级分类数据列表
    async getParent () {
      const { data: res } = await this.$http.get('categories', { params: { type: 2 } })
      if (res.meta.status !== 200) {
        return this.$message.error('获取父级分类数据列表失败')
      }
      this.parentCateList = res.data
    },

    parentCateChanged () {
      if (this.selectedKeys.length > 0) {
        this.addcateForm.cat_pid = this.selectedKeys[this.selectedKeys.length - 1]
        this.addcateForm.cat_level = this.selectedKeys.length
        return true
      } else {
        this.addcateForm.cat_pid = 0
        this.addcateForm.cat_level = 0
      }
    },

    addcates () {
      this.$refs.addcateRef.validate(async v => {
        if (!v) {
          return false
        }
        const { data: res } = await this.$http.post('categories', this.addcateForm)
        if (res.meta.status === 201) {
          this.catedialogVisible = false
        }
      })
      this.getCatelist()
    }
  },
  created () {
    this.getCatelist()
  },
  watch: {
    catedialogVisible (n) {
      if (!n) {
        this.addcateForm = {
          cat_name: '',
          cat_pid: 0,
          cat_level: 0
        }
        this.parentCateList = []
      }
    }
  }
})
</script>

<style scoped>
.treeTable {
  margin-top: 15px;
}
.el-cascader {
  width: 100%;
}
</style>
